import socketio

# 实例化client
sio = socketio.Client()

# sio.connect('https://localhost:3000', namespaces="/channel_A")
sio.connect('http://127.0.0.1:8000', namespaces="/channel_A")


# 测试通过：且优先MyCustomClientNamespace执行
# sio.emit('messages', 'juran', namespace='/channel_A')


def outer_func():
    print('*************on_outer_func********************')
    sio.emit('messages', 'juran', namespace='/channel_A')
    print('*************on_outer_func_end********************')
    pass


class MyCustomClientNamespace(socketio.ClientNamespace):
    # 不能写
    # def __init__(self, socketio, namespace=None):
    #     super().__init__(namespace=namespace)
    #     self.socketio = socketio

    def on_connect(self):
        print('****on_connect***I \'m client  connect to Server')
        # 不是：on_myEmail
        # 测试1通过：
        # sio.emit('myWechat', {'data': 'juran send'}, namespace="/channel_A")
        # 测试2通过：self.emit()
        # self.emit('myWechat', {'data': 'juran send'}, namespace="/channel_A")
        pass

    def on_disconnect(self):
        pass

    def on_custom_messages(self, data):
        # 测试3通过：
        print('/////')
        print(str(data))
        # print('*********on_custom_messages***********')
        # 测试4通过：调用class内func()
        # self.on_inner_func()
        # 测试5通过，调用class外func()
        # outer_func()

    def on_inner_func(self):
        print('*************on_inner_func********************')
        self.emit('response', 'juran', namespace="/channel_A")
        pass


# # 注意:Flask-Socketio用on_namespace;Python-Socketio用register_namespace
sio.register_namespace(MyCustomClientNamespace('/channel_A'))
